package com.yiren.dbaa.module.project_edit.presenter;

import android.app.Activity;
import android.content.DialogInterface;

import com.google.gson.Gson;
import com.yiren.backstage.currency.entity.LightCombinationEntity;
import com.yiren.backstage.currency.entity.SingleLightEntity;
import com.yiren.backstage.currency.entity.SiteEntity;
import com.yiren.backstage.currency.utils.StringUtil;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.module.project_edit.contract.GroupLightContract;
import com.yiren.dbaa.module.project_edit.contract.SingleLightContract;
import com.yiren.dbaa.module.project_edit.model.GroupLightModel;
import com.yiren.dbaa.module.project_edit.model.SingleLightModel;
import com.yiren.dbaa.view.adapter.SingleLightAdapter;
import com.yiren.dbaa.view.dialog.CustomDialog;
import com.yiren.dbaa.view.dialog.GroupLightSelectDialog;
import com.yiren.dbaa.view.toast.MessageBox;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lwb
 * @date : 2022/8/18 14:27
 * @description: 灯光组合
 */
public class GroupLightPresenter implements GroupLightContract.Presenter {

    private Activity activity;
    private GroupLightContract.Model groupLightModel;
    private SingleLightContract.Model singleModel;
    private GroupLightContract.View view;

    public GroupLightPresenter(Activity activity) {
        this.activity = activity;
        groupLightModel = new GroupLightModel(activity);
        singleModel = new SingleLightModel(activity);
    }

    @Override
    public void loadGroupLightList(GroupLightContract.View view) {
        this.view = view;
        List<LightCombinationEntity> lightCombinationList = groupLightModel.getAllLightCombinations();
        view.loadGropLight(lightCombinationList);

    }

    @Override
    public void loadGroupLightSingleList(LightCombinationEntity lightCombination) {
        lightCombination.getSingleLightEntities();
    }


    @Override
    public void loadAllSingleLightList() {

    }

    @Override
    public void addGroupLight(SiteEntity site, CommonCallBack callBack) {
        List<LightCombinationEntity> lightCombinationList = groupLightModel.getAllLightCombinations();
        boolean isCanAddGroup = true;
        int selectGroupLightIndex = 0;
        for (int i = 0; i < lightCombinationList.size(); i++) {
            if (lightCombinationList.get(i).getSingleLightEntities() == null || lightCombinationList.get(i).getSingleLightEntities().size() == 0) {
                isCanAddGroup = false;
                if (lightCombinationList.get(i).getSingleLightEntities().size() <= 0) {
                    selectGroupLightIndex = i;
                    callBack.success(selectGroupLightIndex);
                }
            }
        }
        if (isCanAddGroup) {
            LightCombinationEntity lightCombination = new LightCombinationEntity();
            lightCombination.setId(StringUtil.getUUID());
            lightCombination.setSingleLightEntities(new ArrayList<>());
            groupLightModel.insertGroupLight(site, lightCombination);
            List<LightCombinationEntity> list = groupLightModel.getAllLightCombinations();
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getSingleLightEntities().size() <= 0) {
                    selectGroupLightIndex = i;
                }
            }
            callBack.success(selectGroupLightIndex);
        } else {
            callBack.fail(selectGroupLightIndex + "");
            MessageBox.show(activity, "请先添加灯光再新增组合!");
        }
    }

    @Override
    public void deleteGroupLit(SiteEntity site, LightCombinationEntity lightCombination, CommonCallBack callBack) {
        CustomDialog.Builder builder = new CustomDialog.Builder(activity);
        builder.setTitle("温馨提示");
        builder.setMessage("确认删除当前灯光组合？");
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                groupLightModel.deleteGroupLight(site, lightCombination);
                callBack.complete();
                dialog.dismiss();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show();
    }

    @Override
    public void replaceSingleLight(SiteEntity site, LightCombinationEntity lightCombination, int position) {
        List<SingleLightEntity> singleLightList = singleModel.getAllSingleLight();
        GroupLightSelectDialog.Builder builder = new GroupLightSelectDialog.Builder(activity, activity, singleLightList, 4);
        builder.setTitle("选择一项灯光替换");
        builder.setPositiveButton("替换", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                for (int i = 0; i < SingleLightAdapter.sSelectPositionList.size(); i++) {
                    List<String> ids = new Gson().fromJson(lightCombination.getSingleLightIds(), ArrayList.class);
                    ids.set(position, String.valueOf(singleLightList.get(SingleLightAdapter.sSelectPositionList.get(i)).getId()));
                    lightCombination.getSingleLightEntities().set(position, singleLightList.get(SingleLightAdapter.sSelectPositionList.get(i)));
                    lightCombination.setSingleLightIds(new Gson().toJson(ids));
                }
                groupLightModel.updateGroupLight(site, lightCombination);
                view.refreshAdapter(lightCombination);
                dialog.dismiss();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });

        builder.create().show();


    }

    @Override
    public void addSingleLightToGroup(SiteEntity site, List<LightCombinationEntity> lightCombinationList, int selectGroupLightIndex) {
        if (lightCombinationList == null) {
            MessageBox.show(activity, "请先新增组合再添加灯光!");
        } else {
            LightCombinationEntity lightCombination = lightCombinationList.get(selectGroupLightIndex);
            List<SingleLightEntity> singleLightList = singleModel.getAllSingleLight();
            if (singleLightList.size() <= 0) {
                MessageBox.show(activity, "当前暂无单项灯光，请先新增单项灯光!");
                return;
            }
            GroupLightSelectDialog.Builder builder = new GroupLightSelectDialog.Builder(activity, singleLightList, 3);
            builder.setTitle("灯光选择");
            builder.setPositiveButton("添加", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    List<SingleLightEntity> mslist = lightCombination.getSingleLightEntities();
                    List<String> ids = new Gson().fromJson(lightCombination.getSingleLightIds(), ArrayList.class);
                    if (ids == null) {
                        ids = new ArrayList<>();
                    }
                    if (lightCombination.getSingleLightEntities() == null) {
                        lightCombination.setSingleLightEntities(mslist);
                    }
                    for (int i = 0; i < SingleLightAdapter.sSelectPositionList.size(); i++) {
                        mslist.add(singleLightList.get(SingleLightAdapter.sSelectPositionList.get(i)));
                        ids.add(singleLightList.get(SingleLightAdapter.sSelectPositionList.get(i)).getId().toString());
                        lightCombination.setSingleLightIds(new Gson().toJson(ids));
                        lightCombination.setSingleLightEntities(mslist);
                    }
                    groupLightModel.updateGroupLight(site, lightCombination);
                    view.refreshAdapter(lightCombination);
                    dialog.dismiss();
                }
            });
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            builder.create().show();

        }

    }

    @Override
    public void deleteSingleLightToGroup(SiteEntity site, LightCombinationEntity lightCombination, int position) {
        CustomDialog.Builder builder = new CustomDialog.Builder(activity);
        builder.setTitle("温馨提示");
        builder.setMessage("确认删除当前灯光？");
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                lightCombination.getSingleLightEntities().remove(position);
                List<String> ids = new Gson().fromJson(lightCombination.getSingleLightIds(), ArrayList.class);
                ids.remove(position);
                lightCombination.setSingleLightIds(new Gson().toJson(ids));
                groupLightModel.updateGroupLight(site, lightCombination);
                view.refreshAdapter(lightCombination);
                dialog.dismiss();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();

            }
        });
        builder.create().show();
    }

    @Override
    public void moveSingleLight(SiteEntity site, LightCombinationEntity lightCombination, int from, int to) {
        List<SingleLightEntity> mSingleLightList = lightCombination.getSingleLightEntities();
        List<SingleLightEntity> list = new ArrayList<SingleLightEntity>();
        for (int i = 0; i < mSingleLightList.size(); i++) {
            list.add(mSingleLightList.get(i));
        }
        List<String> ids = new Gson().fromJson(lightCombination.getSingleLightIds(), ArrayList.class);
        List<String> idsCopy = new ArrayList<>();
        for (int j = 0; j < ids.size(); j++) {
            idsCopy.add(ids.get(j));
        }
        mSingleLightList.set(from, list.get(to));
        mSingleLightList.set(to, list.get(from));
        ids.set(from, idsCopy.get(to));
        ids.set(to, idsCopy.get(from));
        lightCombination.setSingleLightEntities(mSingleLightList);
        lightCombination.setSingleLightIds(new Gson().toJson(ids));
        groupLightModel.updateGroupLight(site, lightCombination);
        view.refreshAdapter(lightCombination);
    }
}
